$(init);

function init() {
    $('body').on('click', 'button.back-index', backToIndex)
             .on('click', '#return', doReturn)
             .on('click', '#getcode-btn', doGetPwd)
             .on('click', '#changePwd', doChgPwd);
}

function backToIndex() {
    goto('/mobile.html');
}

function doChgPwd() {
    var obj = {
        mobile: $('#mobile-pwd').val(),
        smscode: $('#smscode').val(),
        password: $('#pwd').val()
    };

    type = $("input[name='user-type']:checked").val();
    url='/buyer/replace_buyer_password';

    promise('post',url,JSON.stringify(obj), function(e){
        notifyInfo('密码已经修改成功！');
        doReturn();
    },null);
}

function doReturn() {
    $('#getcode-btn').text('获取验证码');
    $('#getcode-btn').attr("disabled",false);
    backToIndex();
}

function doGetPwd() {
    var obj = { mobile: $('#mobile-pwd').val() };
    type = $("input[name='user-type']:checked").val();
    url='/buyer/mobile_exist';

    promise('get',[url, encodeQuery(obj)].join('?'),null, function(e){
        var obj = { mobilephone: $('#mobile-pwd').val(), forgetPassword: 1 };
        $('#getcode-btn').attr("disabled",true);
        promise('GET', URL_SMS_SEND, obj, function(e){
            notifyInfo('验证码已经发送到您的手机！');
        },null);
        doCounter(CODE_COUNT,'getcode-btn');
    },null);
}

function doCounter(count, id) {
    setTimeout(function() {
        $(`#${id}`).text(--count);
        if (count) {
            doCounter(count, id)
        } else {
            $(`#${id}`).text('获取验证码');
            $(`#${id}`).attr("disabled", false);
        }
    }, 1000);
}